package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ciei.dpagm.common.converter.JsonConverter;
import com.ciei.dpagm.common.enums.GenderEnum;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.Person;
import com.ciei.dpagm.mapper.PersonMapper;
import com.ciei.dpagm.util.*;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class PersonService extends BaseService<PersonMapper, Person> {

    public JSONObject loadPersonData(int posStart, int pageSize, Map<String, Object> params, String[] likeFields, String[] equalFields) {
        List<Person> list = findPage(posStart, pageSize, params, likeFields, equalFields, "person_name asc");

        JSONObject json = new JSONObject();
        JSONArray jarr = new JSONArray();
        int index = posStart+1;
        for (Person res : list) {
            Map<String, Object> mobj = new HashMap<String, Object>();

            mobj.put("personId", res.getPersonId());
            mobj.put("rowNumber", index++);
            mobj.put("personName", res.getPersonName());
            mobj.put("gender", res.getGender());
            mobj.put("email", res.getEmail());
            mobj.put("mobileTel", res.getMobileTel());
            mobj.put("telephone", res.getTelephone());
            mobj.put("userNote", res.getUserNote());
            mobj.put("id",res.getPersonId());
            mobj.put("createTime",(res.getCreateTime()==null?"": DateUtil.dateToString(res.getCreateTime(),"yyyy-MM-dd HH:mm")));
            jarr.add(mobj);
        }
//		int totalCount = baseDAO.getTotalCount(SysLog.class,params, likeFields, equalFields);
        int totalCount = 	getTotalCount(params,likeFields,equalFields);
        json.put(APP.DATA, jarr);
        json.put(APP.TOTAL_COUNT, totalCount);
        json.put(APP.POS, posStart);
        return json;
    }
    public JSONObject loadPersonDataVue(PageParam ps) {
        Map<String, Object> params=ps.getParams();
        params.put("isDelete", 0);
        params.put("companyId", params.get("companyId"));
        Map<String,Object> sortFields= ps.getSort();
        if(sortFields.isEmpty()){
            sortFields.put("person_name","asc");
        }
        JSONObject json = new JSONObject();
        int currentPage =ps.getCurrentPage();
        int pageSize =ps.getPageSize();
        IPage<Person> page = findPageVue(currentPage, pageSize, params, new String[]{"personName"}, new String[]{"isDelete","companyId","personType"}, sortFields,null);
        List<Person> list= page.getRecords();
        JSONArray jarr = new JSONArray();
        for (Person res : list) {
            Map<String, Object> mobj = new HashMap<String, Object>();
            mobj.put("personId", res.getPersonId());
            mobj.put("personName", res.getPersonName());
            Optional<GenderEnum> m1 = EnumUtil.getEnumObject(GenderEnum.class, g -> g.getGenderId().equals(res.getGender()));
            mobj.put("sex", m1.isPresent() ? m1.get().getGender() : null);
            mobj.put("email", res.getEmail());
            mobj.put("mobileTel", res.getMobileTel());
            mobj.put("telephone", res.getTelephone());
            mobj.put("userNote", res.getUserNote());
            mobj.put("post", res.getPost());
            jarr.add(mobj);
        }
        json.put(APP.DATA, jarr);
        json.put(APP.TOTAL_COUNT, page.getTotal());
        json.put(APP.POS, (currentPage - 1) * pageSize);
        json.put(APP.CURRENT_PAGE, page.getCurrent());
        return json;
    }

    public JSONObject addPerson(Person person) {
        JSONObject json;
        if(save(person)==true){
            json= JsonUtil.getSuccess("Success");
            json.put(APP.DATA,person);
        }else{
            json= JsonUtil.getFail("增加失败");
        }
        return json;

    }
    public JSONObject addOrUpdatePerson(JSONObject jsonObject,String type) {
        JSONObject json = null;
        JsonConverter<Person> jsonConverter=new JsonConverter<>();
        Person person = new Person();
        //sex
        person = jsonConverter.convert(jsonObject, person);
        String sex = jsonObject.getString("sex");
        Optional<GenderEnum> m1 = EnumUtil.getEnumObject(GenderEnum.class, g -> g.getGender().equals(sex));
        person.setGender(m1.isPresent() ? m1.get().getGenderId() : null);
        if("add".equals(type)){
            save(person);
            json = JsonUtil.getSuccess("添加成功");
        }else{
            updateById(person);
            json = JsonUtil.getSuccess("更新成功");
        }
        return json;
    }

    public JSONObject deleteVue(JSONObject jsonObject){
        JSONObject json;
        if(jsonObject.getInteger("personId")!=null){
            Person person = new Person();
            removeById(jsonObject.getInteger("personId"));
            json = JsonUtil.getSuccess("删除成功!");
        }else{
            json = JsonUtil.getFail("删除失败,请选择要删除的数据");
        }
        return json;
    }

    public  JSONObject updatePerson(Person person){
        JSONObject json;
        if(person.getCreateTime() != null){
            person.setCreateTime(new Timestamp(System.currentTimeMillis()));
        }


        if(updateById(person)==true){
            json= JsonUtil.getSuccess("Success");
            json.put(APP.DATA,person);
        }else{
            json=JsonUtil.getFail("增加失败");
        }
        return json;
    }

}

